/**
 * Created by wang on 2016-3-10.
 * gulp 打包插件
 */

var gulp = require('gulp');
var amdOptimize = require("amd-optimize");
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var replace = require('gulp-replace');
var clean = require('gulp-clean');
var minifyHtml = require("gulp-minify-html");
var minifyCss = require("gulp-minify-css");
var minimist = require('minimist');
//var gulpif = require('gulp-if');
//var rename = require('gulp-rename');

var distPath = 'dist';
var tempPath = 'temp';
var knownOptions = {
    string: 'env',
    default: { env: process.env.NODE_ENV || 'uat' }
};

var options = minimist(process.argv.slice(2), knownOptions);

gulp.task('clean', function() {
    return gulp.src([distPath], {read:false})
        .pipe(clean());
});

gulp.task('commonJS',['clean'], function() {
    return gulp.src(['js/common/*.js'])
        .pipe(uglify())
        .pipe(gulp.dest(distPath + '/gcs/js/common'));
});

gulp.task('mainJS',['commonJS'], function() {
    return gulp.src(['js/main.js'])
        .pipe(uglify())
        .pipe(gulp.dest(distPath + '/gcs/js'));
});

gulp.task('cleanTemp', function() {
    return gulp.src([tempPath], {read:false})
        .pipe(clean());
});

gulp.task('copyJs',['cleanTemp'], function() {
    return gulp.src(['js/**/*.js'])
        .pipe(gulp.dest(tempPath + '/gcs/js/'));
});


gulp.task('copyHtml',['cleanTemp'], function() {
    return gulp.src(['view/**/*.html'])
        .pipe(gulp.dest(tempPath + '/gcs/view'));
});

gulp.task('deploy',['copyHtml','copyJs'], function() {
    if(options.env === 'dit'){
        return gulp.src(['../../deploy/dit/**/**/*.*'])
            .pipe(gulp.dest(tempPath + '/gcs/'));
    }else if(options.env === 'preprod'){
        return gulp.src(['../../deploy/preprod/**/**/*.*'])
            .pipe(gulp.dest(tempPath + '/gcs/'));
    }else if(options.env === 'prod'){
        return gulp.src(['../../deploy/prod/**/**/*.*'])
            .pipe(gulp.dest(tempPath + '/gcs/'));
    }else if(options.env === 'uat'){
        return gulp.src(['../../deploy/uat/**/**/*.*'])
            .pipe(gulp.dest(tempPath + '/gcs/'));
    }else if(options.env === 'uat3'){
        return gulp.src(['../../deploy/uat3/**/**/*.*'])
            .pipe(gulp.dest(tempPath + '/gcs/'));
    }else {
        return gulp.src(['../../deploy/uat2/**/**/*.*'])
            .pipe(gulp.dest(tempPath + '/gcs/'));
    }
});

gulp.task('bootstrapJS',['mainJS','deploy'], function() {
    return gulp.src(tempPath + '/gcs/js/**/*.js')
        // .pipe(amdOptimize("bootstrap",  {
        //     paths: {
        //         angular:          '../common/ionic/js/angular/angular.min',
        //         ngCookies:        '../common/ionic/js/angular/angular-cookies.min', //动态样式加载
        //         angularAnimate:   '../common/ionic/js/angular/angular-animate.min',
        //         angularSanitize:  '../common/ionic/js/angular/angular-sanitize.min',
        //         uiRouter:         '../common/ionic/js/angular-ui/angular-ui-router.min',
        //         ngCordova:        '../common/ionic/js/angular/ng-cordova.min',
        //         angularCss:       '../common/ionic/js/angular/angular-css.min', //动态样式加载
        //         ngmodelFormat:    '../common/ionic/js/angular/ngmodel.format.min', //输入框格式化
        //         angularKeepValues:'../common/ionic/js/angular/angular-keep-values.min', //模板初始化值
        //         'angular-cache':     '../common/ionic/js/angular/angular-cache.min',
        //         angularOffline:   '../common/ionic/js/angular/angular-offline.min',
        //         ionic:            '../common/ionic/js/ionic.min',
        //         ionicAngular:     '../common/ionic/js/ionic-angular.min',
        //         topScroller:     '../common/ionic/js/topscroller.min',
        //         text:             '../common/ionic/js/text.min',
        //         jquery:           '../common/jquery/jquery',
        //         jqueryCookie:     '../common/jquery/jquery.cookie',
        //         featureCarousel:  '../common/jquery/jquery.featureCarousel',
        //         highstock:        '../common/highstock/highstock',
        //         highchartsNg:     '../common/ionic/js/angular/highcharts-ng.min', // AngularJS directive for Highcharts
        //         store:            '../common/jquery/store.min',
        //         xdate:            '../gcs/js/common/xdate',
        //         base:             '../common/js/jkf/base',
        //          bindOnce:         '../common/bindonce/bindonce.min',
        //         radialIndicator:  '../common/radialIndicator/radialIndicator.min'
        //     },
        //     shim: {
        //         jquery : {exports : 'jquery'},
        //         jqueryCookie : {deps: ['jquery'], exports : 'jqueryCookie'},
        //         featureCarousel:{deps:['jquery']},
        //         highstock : {deps: ['jquery'], exports : 'highstock'},
        //         base : {deps: ['jquery'], exports : 'base'},
        //         store : {deps: ['jquery'], exports : 'store'},
        //         xdate : {deps: ['jquery'], exports : 'xdate'},
        //         angular : {exports : 'angular'},
        //         angularAnimate : {deps: ['angular']},
        //         angularSanitize : {deps: ['angular']},
        //         ngCookies : {deps: ['angular']},
        //         uiRouter : {deps: ['angular']},
        //         ngCordova : {deps: ['angular']},
        //         ngmodelFormat : {deps: ['angular']},
        //         angularKeepValues : {deps: ['angular']},
        //         angularOffline : {deps: ['angular']},
        //         bindOnce:{deps:['angular']},
        //         topScroller :  {deps: ['angular'], exports : 'topScroller'},
        //         angularCss : {deps: ['angular','uiRouter']},
        //         highchartsNg : {deps: ['angular','highstock'], exports : 'highchartsNg'},
        //         ionic :  {deps: ['angular'], exports : 'ionic'},
        //         ionicAngular: {deps: ['angular', 'ngCordova','ionic', 'uiRouter', 'angularAnimate', 'angularSanitize','angularCss',
        //             'ngmodelFormat','angularKeepValues','highchartsNg','ngCookies','angularOffline','topScroller']},
        //         radialIndicator:{deps:['jquery']}
        //     },
        //     findNestedDependencies: true,
        //     exclude:['xdate','common/distpicker.min','ionicAngular','store','angular-cache','jqueryCookie','base','highchartsNg','jquery']
        // }))
        // .pipe(concat('bootstrap.js'))
        // .pipe(uglify())
        // .pipe(replace(/n\.config\(function\(n,r\)\{/g, 'n.config(function($httpProvider,$ionicConfigProvider){var n=$httpProvider,r=$ionicConfigProvider;'))
        .pipe(gulp.dest(distPath+'/gcs/js/'));
});

gulp.task('html',['deploy','clean'], function() {
    return gulp.src([tempPath + '/gcs/view/**/**/*.html'])
        // .pipe(minifyHtml())
        .pipe(gulp.dest(distPath + '/gcs/view'));
});

gulp.task('delete',['html','bootstrapJS'], function() {
    return gulp.src([tempPath], {read:false})
        .pipe(clean());
});

gulp.task('css',['clean'], function () {
    gulp.src('css/**/*.css')
        // .pipe(minifyCss())
        .pipe(gulp.dest(distPath + '/gcs/css'));
});

gulp.task('img',['clean'], function () {
    gulp.src('img/**')
        .pipe(gulp.dest(distPath + '/gcs/img'));
});

gulp.task('default', [], function() {
    gulp.start('delete','css','img');
});